/*
逻辑控制，增删改查
*/ 
const mongoose=require('mongoose');
const User=require('../models/user.model');

exports.get =function(req,res,next){
    const id =req.params.id;
    User.findById(id,function(err,data){
        res.json(data);
    })
}


/*增 */
exports.create=function(req,res,next){
        const user=new User(req.body);
        user.save()
        .then(data=>{
            res.json(data)
        })
}
/*修*/
exports.update=function(req,res,next){
    const id=req.params.id;
    const user=new  User(req.body);
    User.findByIdAndUpdate(id,{$set:req.body},{new:false})
    .then(data=>{
        res.json(data);
    })
}




/*删 */

exports.remove=function(req,res,next){
    const id=req.params.id;
    
    User.findByIdAndRemove(id,function(err,data){
            res.json({'massage':'delete success'})
    })
    
}
/*删除多个 */

exports.removes=function(req,res,next){
    var ids=req.body.ids;
    ids=ids.split(',');
    if (ids.length>0) {
        User.remove({_id:{$in:ids}})
        .then(data=>{
            res.json({'message':'多条数据已删除'})
        })
    } else {
        res.status(404).send({'massage':'404'})
    }

    
}

/*查*/
exports.list=function(req,res,next){
    var page=(req.body.page)?req.body.page:1;
    var rows=(req.body.rows)?req.body.rows:1;
    var queryCondition={};
    if(req.body.name && req.body.name.trim().length>0){
        name=req.body.name;
        queryCondition={
            'name':new RegExp(name,'i')
        }
    }

    User.paginate(queryCondition,{page:+page,rows:+rows},function(err,result){
        //res.json(result)

        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    })

}